#include"Contact.h"

Phone* createPhone()
{
	Phone* newNode = (Phone*)malloc(sizeof(Phone));
	printf("enter name\n");
	scanf("%s", newNode->name);
	printf("enter telephone\n");
	scanf("%s", newNode->telephone);
	newNode->next = NULL;
	return newNode;
}

int addRecord(Phone* head)
{
	Phone* newPhone = createPhone();
	Phone* cur = head;
	int count = 0;
	while (cur->next != NULL)
	{
		cur = cur->next;
		count++;
	}
	cur->next = newPhone;
	count++;
	return count;
}

void display(Phone* head)
{
	printf("name\t\tprivate\t\toffice\t\t\n");
	Phone* cur = head->next;
	while (cur != NULL)	
	{						
		char tel[6][15] = { 0 };
		splitTelPhone(cur->telephone, tel);
		printf("%-16s", cur->name);
		
		int i = 0;
		while(i<6)
		{
			int flagP = 0;
			int flagQ = 0;
			if (tel[i][0] == 0)
			{
				printf("\n");
				break;
			}
			for (int j = i; j < 6; j++)
			{
				if (tel[j][0] == 'P')
				{
					char* p = tel[i];
					p++;
					printf("%-16s", p);
					i++;
					flagP = 1;
					break;
				}
			}
			if (flagP == 0) printf("\t\t");
			for (int j = i; j < 6; j++)
				if (tel[j][0] == 'Q')
				{
					char* q = tel[j];
					q++;
					printf("%-16s\n", q);
					i++;
					flagQ = 1;
					break;
				}
			if (flagQ == 0) printf("\n");
			printf("\t\t");
		}
		cur = cur->next;
	}
}

int deletePhone(Phone* head)
{
	if (head->next == NULL)
	{
		printf("Empty contact\n");
		return 0;
	}
	printf("Which phone to delete? Please input its name\n");
	char name[MAXNAME] = {0};
	scanf("%s", name);
	Phone* cur = head->next;
	Phone* curPre = head;
	int count = 1;
	int flag = 0;
	while (cur != NULL)
	{
		if (strcmp(cur->name,name)==0)
		{
			curPre->next = cur->next;
			free(cur);
			cur = curPre->next;
			count--;
			flag = 1;
		}
		else
		{
			cur = cur->next;
			curPre = curPre->next;
			count++;
		}
	}
	if (flag == 0) printf("NO name\n");
	return count;
}

bool modifyPhone(Phone* head)
{
	Phone* cur = head->next;
	if (cur == NULL) return false;
	char name[MAXNAME] = { 0 };
	printf("please input whose phone need to be modified\n");
	scanf("%s", name);
	while (cur != NULL)
	{
		if (strcmp(cur->name, name)==0)
		{
			printf("please input the new telephone\n");
			scanf("%s", cur->telephone);
			return true;
		}
		cur = cur->next;
	}
	return false;
}

int saveData(Phone* head, char* fileName)
{
	FILE* pWrite = fopen(fileName, "wb");
	if (pWrite == NULL)
	{
		printf("file open failed\n");
		exit(-1);
	}
	fwrite(head, sizeof(Phone), 1, pWrite);
	fclose(pWrite);

	pWrite = fopen(fileName, "ab");
	Phone* cur = head->next;
	int count = 0;
	while (cur != NULL)
	{
		fwrite(cur,sizeof(Phone),1,pWrite);
		count++;
		cur = cur->next;
	}
	fclose(pWrite);
	return count;
}


Phone* readData(char* fileName,int* num)
{
	FILE* pRead = fopen(fileName, "rb");
	Phone* head = (Phone*)malloc(sizeof(Phone));
	size_t flag = fread(head, sizeof(Phone), 1, pRead);
	Phone* cur = head;
	Phone* curNext = (Phone*)malloc(sizeof(Phone));
	flag = fread(curNext, sizeof(Phone), 1, pRead);
	while(flag == 1)
	{
		cur->next = curNext;
		(*num)++;
		cur = cur->next;
		curNext = NULL;
		curNext = (Phone*)malloc(sizeof(Phone));
		flag = fread(curNext, sizeof(Phone), 1, pRead);
	}
	fclose(pRead);
	return head;
}

int splitTelPhone(char s[], char tel[][15])
{
	int i = 0;//number of tel
	int count = 0;
	while (*s != 0 && i<6)
	{
		if (*s == 'P' || *s == 'Q')
		{
			int j = 0;
			count++;
			tel[i][j] = *s;
			s++;
			j++;
			while (*s != 'P' && *s != 'Q' && *s!=0 && j<15)
			{
				tel[i][j] = *s;
				j++;
				s++;
			}
			i++;
		}
		else s++;
	}
	return count;
}

void menu()
{
	printf("******************\n");
	printf("*** 1.add     ****\n");
	printf("*** 2.delete  ****\n");
	printf("*** 3.modify  ****\n");
	printf("*** 4.display ****\n");
	printf("*** 5.exit    ****\n");
	printf("******************\n");
	printf("Choose your operation.\n");
}

void mainMenu()
{
	Phone* head = (Phone*)malloc(sizeof(Phone));
	head->next = NULL;
	int num = 0;
	head = readData(FPHONE, &num);
	int operation = 0;
	menu();
	scanf("%d", &operation);
	while (operation != EXIT)
	{
		switch (operation)
		{
		case ADD:addRecord(head, &num); break;
		case DELETE:deletePhone(head); break;
		case MODIFY:modifyPhone(head); break;
		case DISPLAY:display(head); break;
		case EXIT:return;
		defalut:printf("Input ERROR\n");
		}
		menu();
		scanf("%d", &operation);
	}
	saveData(head,FPHONE);
	return;
}


